using System;
using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;

namespace RoleEditer
{
    public class ANIPlay : Silvery.NImageD
    {
        //判断是否能够控制，如果正在播放或者列表为空为false
        public bool IsCanControl
        {
            get
            {
                if (IsPlaying || nowPlayList == null)
                    return false;
                else
                    return true;
            }
        }
        List<Image> nowPlayList = null;
        //正在播放的列表
        public List<Image> NowPlayList
        {
            get { return nowPlayList; }
            set
            {
                nowPlayList = value;
                playFramesIndex = 0;
                this.Content.Source = null;
            }
        }
        //播放速度，毫秒计算
        public double Millisecond
        {
            get { return base.SelfLoopInterval.TotalMilliseconds; }
            set
            {
                base.SelfLoopInterval = TimeSpan.FromMilliseconds(value);
            }
        }
        
        static ANIPlay current;
        //单例
        public static ANIPlay Current
        {
            get
            {
                if (current == null)
                    current = new ANIPlay();
                return current;
            }
        }
        //开始播放
        private ANIPlay()
        {
            base.IsSelfLoopRun = true;
        }
        
        private int playFramesIndex;
        //当前播放的帧数
        public int PlayFramesIndex
        {
            get { return playFramesIndex; }
            set
            {
                if (NowPlayList != null)
                {
                    if (NowPlayList.Count == 0)
                        return;
                    playFramesIndex = value;
                    if (playFramesIndex >= NowPlayList.Count)
                        playFramesIndex = 0;
                    var img = NowPlayList[playFramesIndex];
                    Content.Source = img.Source;
                    MainPage.Current.SetFrameFromIndex(playFramesIndex);
                    if (img.Tag != null)
                    {
                        Point p = (Point)img.Tag;
                        if (p != null)
                        {
                            OffsetPoint = p;
                            MainPage.Current.ChangeOffsetValue(new Point(-p.X,-p.Y));
                        }
                    }
                }
            }
        }
        //刷新偏移点
        public void UpdataOffset()
        {
            if (IsCanControl == false)
                return;
            var img = NowPlayList[playFramesIndex];
            if (img.Tag != null)
            {
                Point p = (Point)img.Tag;
                if (p != null)
                {
                    OffsetPoint = p;
                }
            }
        }
        //刷新偏移点,带参数，所有的
        public void UpdataOffset(Point all)
        {
            if (IsCanControl == false)
                return;
            foreach (var item in NowPlayList)
            {
                item.Tag = all;
            }
            UpdataOffset();
        }
        //是否正在播放
        public bool IsPlaying { get; set; }
        public override void OnUpdata(Silvery.BaseObject sender, Silvery.ITimer timer)
        {
            if (IsPlaying)
            {
                PlayFramesIndex += 1;
            }
            base.OnUpdata(sender, timer);
        }
    }
}
